<template>
    <section>
        <div class="section-content clearfix"> 
            <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">  
                <el-form :inline="true" :model="filters">
                    <el-form-item style="float: left;">
                        <el-input v-model="filters.name" placeholder="单位名称" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <el-form-item class="tool-bt">
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>
                    <el-form-item >
                        <el-button :type="buttonColor" @click="handleAdd">新增</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--列表-->
        <el-table :data="query" highlight-current-row v-loading="listLoading" :header-row-style="{backgroundColor: titleColor}" style="width: 100%;">
                <el-table-column type="index" label="序号" width="80" :index="indexMethod">
                </el-table-column>
                <el-table-column prop="name" label="名称" >
                </el-table-column>               
                <el-table-column prop="value" label="单位" >
                </el-table-column>
                <el-table-column   prop="priority" label="排序" >
                </el-table-column>
                <el-table-column   prop="remark" label="描述" >
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template scope="scope">
                            <i @click="handleEdit(scope.$index, scope.row)" title="编辑" icon="el-icon-edit"
                            circle class="pb-icon icon-edit"></i>
                            <i @click="handleDel(scope.$index, scope.row)" icon="el-icon-delete"
                            circle title="删除" class="pb-icon icon-delete"></i>
                    
                    </template>
                </el-table-column>
            </el-table>
            <!--工具条-->
        <el-col :span="24" class="toolbar">
            <!-- <el-button v-if="menubt.indexOf('/dict/delete')>-1" type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total"
                style="float:right;">
            </el-pagination>
        </el-col>
        </div>

         <!--编辑界面-->
        <el-dialog title="编辑" v-show="editFormVisible" v-model="editFormVisible" :custom-class="dialogTitleColor" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                    <el-form-item label="名字" prop="name">
                        <el-input v-model="editForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="单位" prop="value">
                        <el-input v-model="editForm.value" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="优先级" prop="priority">
                        <el-input v-model="editForm.priority" type="number" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input type="textarea" v-model="editForm.remark"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align:right;">
                    <el-button @click.native="editFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="editLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>

         <!--新增界面-->
        <el-dialog title="新增" v-show="addFormVisible" v-model="addFormVisible" :custom-class="dialogTitleColor" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form :model="addForm" label-width="100px" :rules="addFormRules" ref="addForm">
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="单位" prop="value">
                        <el-input v-model="addForm.value" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="优先级" prop="priority">
                        <el-input v-model="addForm.priority" type="number" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="addForm.remark" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="addSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>

    </section>
</template>

<script>
    import {
        unit_query,
        unit_post,
        unit_put,
        unit_get,
        unit_delete,
    } from "../../api/api";

    export default{
        data() {
            return{
                editFormVisible:false,//编辑界面
                addFormVisible:false,//显示新增框
                listLoading: false,//是否加载中
                editLoading:false,
                addLoading:false,
                page:1,
                total:0,
                query:[],
                filters: {
                    name: ''
                },//按名字查询
                addForm:{
                    name:'',
                    value:'',
                    priority:'',
                    remark:''
                },//新增框传的值
                editForm:{
                    name:'',
                    value:'',
                    priority:'',
                    remark:''
                },
                addFormRules:{
                    name:[{
                     required: true,
                        message: '请输入名称',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
                    ],
                    value:{ 
                        required: true,
                        message: '请输入值',
                        trigger: 'blur'
                    },
                    priority:{ 
                        required: true,
                        message: '请输入优先值',
                        trigger: 'blur'
                    }
                },//新增框的规则
                editFormRules:{
                    name:[{
                     required: true,
                        message: '请输入名称',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' },
                    ],
                    value:{ 
                        required: true,
                        message: '请输入值',
                        trigger: 'blur'
                    },
                    priority:{ 
                        required: true,
                        message: '请输入优先值',
                        trigger: 'blur'
                    }
                }//编辑框的规则
            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods:{
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            handleCurrentChange(val) {
                this.page = val;
                console.log(val)
                this.getUsers();
            },
             handleAdd: function () { 
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    remark: ''
                };//显示新增界面
            },
            handleEdit:function(index,row) {
                this.editFormVisible = true
                let para = {
                    id:row.id
                }
                unit_get(para).then((res) => {
                    this.begCode (res.code);
                    if (res.code == 0) {
                        this.editForm = res.data
                    }
                })
            },//显示编辑界面

            editSubmit : function(){
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true;
                            
                            let para = Object.assign({}, this.editForm);
                                // console.log(para)
                            unit_put(para).then((res) => { this.begCode (res.code);
                                if (res.code == 0) {
                                    this.editLoading = false;
                                   
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    this.$refs['editForm'].resetFields();
                                    this.editFormVisible = false;
                                    this.getUsers();
                                }
                            });
                        });
                    }
                });
            },//编辑提交

            handleDel:function(index,row) {
                 this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                   
                    let para = {
                        id: row.id
                    };
                //    console.log(row)
                   unit_delete(para).then((res) => {
                       this.listLoading = false;
                       this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        this.getUsers();
                    //    console.log(res)
                   })
                }).catch(() => {

                });
            },
            
            addSubmit(){
                 this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            // console.log(para)
                            unit_post(para).then((res) => {
                                console.log(res)
                                this.addLoading = false;
                                 this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false
                                 this.getUsers()
                            })
                        });
                    }
                });
            },//新增的提交事件
            getUsers(){
                this.listLoading = true;
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    name: this.filters.name
                };
                 
                unit_query(para).then( (res) => {
                     this.listLoading = false;
                    console.log(res)
                    this.query = res.data.list
                    this.total = res.data.totalCount;
                })
            }
        },
        mounted(){
            this.getUsers()
        }
    }
</script>